#define _CRT_SECURE_NO_WARNINGS
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
#include <cstddef>
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        ListNode* greathead, * greattail;
        greathead = greattail = (ListNode*)malloc(sizeof(ListNode));
        ListNode* lesshead, * lesstail;
        lesshead = lesstail = (ListNode*)malloc(sizeof(ListNode));
        ListNode* pcur = pHead;
        while (pcur)
        {
            if (pcur->val < x)
            {
                lesstail->next = pcur;
                lesstail = lesstail->next;
            }
            else {
                greattail->next = pcur;
                greattail = greattail->next;
            }
            pcur = pcur->next;
        }
        greattail->next = NULL;
        lesstail->next = greathead->next;
        ListNode* ret = lesshead->next;
        free(greathead);
        free(lesshead);
        greathead = lesshead = NULL;
        return ret;

    }
};